## creates tables for appendix C:
## issue opinion plots
## figures A1, A2, A3, A4, A5, A6

rm(list=ls())

library(tidyverse)

# read in and clean data

opinion_bootstrapped <- readRDS('bootstrap_gaps_data/opinion_bootstrapped_noleaners.RDS')

for (i in names(opinion_bootstrapped)) {
  assign(i, opinion_bootstrapped[[i]])
}

dem_polarization_boot <- dem_top_boot - dem_bottom_boot
rep_polarization_boot <- rep_top_boot - rep_bottom_boot
all_polarization_boot <- all_top_boot - all_bottom_boot

dem_issue_polarization <- abs(apply(dem_polarization_boot, MARGIN = 1, median))
rep_issue_polarization <- abs(apply(rep_polarization_boot, MARGIN = 1, median))
all_issue_polarization <- abs(apply(all_polarization_boot, MARGIN = 1, median))

dem_top_op <- apply(dem_top_boot, MARGIN = 1, median)
dem_bot_op <- apply(dem_bottom_boot, MARGIN = 1, median)
rep_top_op <- apply(rep_top_boot, MARGIN = 1, median)
rep_bot_op <- apply(rep_bottom_boot, MARGIN = 1, median)

diff_polarization_boot <- abs(rep_polarization_boot) - abs(dem_polarization_boot)
diff_polarization_by_issue <- apply(diff_polarization_boot, MARGIN = 1, median)

issueideology <- read.csv('issue_ideology.csv') %>% 
  select(question, year, liberal)


# all figures - make plots

party_pol_summary <- as.data.frame(diff_polarization_by_issue) %>% 
  rownames_to_column('question') %>% 
  left_join(issuetopics) %>% 
  arrange(diff_polarization_by_issue) %>% 
  mutate(question = sub('_', ': ', question, fixed = TRUE))

op_plot.df <- data.frame(question = names(dem_top_op),
                         dem_top = dem_top_op,
                         dem_bot = dem_bot_op,
                         rep_top = rep_top_op,
                         rep_bot = rep_bot_op) %>% 
  left_join(issuetopics %>% 
              select(question, topic_6) %>% 
              distinct) %>% 
  left_join(issueideology) %>% 
  gather(key = group, value = opinion, dem_top, dem_bot, rep_top, rep_bot) %>% 
  mutate(question = sub('_', ': ', question, fixed = TRUE)) %>% 
  left_join(party_pol_summary %>% 
              select(question, diff_polarization_by_issue) %>% 
              distinct())

plotlist2 <- list()

for (i in unique(op_plot.df$topic_6)) {
  tmp.df <- op_plot.df %>% 
    filter(topic_6 == i) %>% 
    arrange(-diff_polarization_by_issue) %>% 
    mutate(question = factor(question, levels = unique(question)),
           opinion = case_when(liberal == 0 ~ 1 - opinion,
                               liberal == 1 ~ opinion))
  
  tmp.plot <- ggplot(tmp.df,
                     aes(x = opinion, y = question)) +
    geom_point(aes(color = group, shape = group), alpha = 0.75) +
    labs(color = '', shape = '',
         y = '', x = 'In-Group Support for Liberal Policy') +
    xlim(min(op_plot.df$opinion), max(op_plot.df$opinion)) + 
    theme_bw() +
    theme(legend.position = 'bottom',
          axis.title.x = element_text(size = 8),
          axis.text = element_text(size = 8),
          legend.margin = margin(l = -100)) +
    scale_color_manual(breaks = c('dem_top', 'dem_bot', 'rep_top', 'rep_bot'),
                       values = c('blue', 'blue', 'red', 'red'),
                       labels = c('Top-Decile\nDemocrat', 'Bottom-Decile\nDemocrat',
                                  'Top-Declle\nRepublican', 'Bottom-Decile\nRepublican')) +
    scale_shape_manual(breaks = c('dem_top', 'dem_bot', 'rep_top', 'rep_bot'),
                       values = c(16, 1, 16, 1),
                       labels = c('Top-Decile\nDemocrat', 'Bottom-Decile\nDemocrat',
                                  'Top-Declle\nRepublican', 'Bottom-Decile\nRepublican'))
  
  plotlist2[[i]] <- tmp.plot
}

for (i in names(plotlist2)) {
  ggsave(paste0('appendix_figures/opinion_points_', i, '.png'), plot = plotlist2[[i]],
         width = 8, height = 10, scale = 0.7)
}
